package de.cbf.cam.dice.entities;

import java.util.ArrayList;
import java.util.List;

import de.cbf.util.entities.IntStringList;
import de.cbf.util.modifier.entities.OneModifier;
import de.cbf.util.modifier.entities.OneModifierMultiply;

public class RollDataModifier extends RollData{

	public static final int PropertyIndexModifiers		 = 5;

	protected List<OneModifier> modifiers ;

	public RollDataModifier() {
		super();
		modifiers = new ArrayList<OneModifier>();
	}

	public boolean addModifier(OneModifier modifier){
		boolean result = modifiers.add(modifier);
		IntStringList intStringList = new IntStringList();
		intStringList.intValue 		= getModifierValue();
		intStringList.stringList 	= getModifierText();
		propertyChange.fireIndexedPropertyChange("Modifiers",PropertyIndexModifiers,null,intStringList);
		return result;
	}
	public boolean removeModifier(OneModifier modifier){
		boolean result = modifiers.remove(modifier);
		if(result){
			IntStringList intStringList = new IntStringList();
			intStringList.intValue 		= getModifierValue();
			intStringList.stringList 	= getModifierText();
			propertyChange.fireIndexedPropertyChange("Modifiers",PropertyIndexModifiers,null,intStringList);
		}
		return result;
	}
	public boolean removeModifierByType(Integer type){
		boolean success = false;
		for (OneModifier modifier : modifiers) {
			if(modifier.getModifierType() == type){
				success = success && modifiers.remove(modifier);
			}
		}
		IntStringList intStringList = new IntStringList();
		intStringList.intValue 		= getModifierValue();
		intStringList.stringList 	= getModifierText();
		propertyChange.fireIndexedPropertyChange("Modifiers",PropertyIndexModifiers,null,intStringList);
		return success;
	}
	public List<OneModifier> getModifiers() {
		return modifiers;
	}
	public List<String> getModifierText(){
		List<String> resultList = new ArrayList<String>();
		for (OneModifier modifier : modifiers) {
			if (modifier instanceof OneModifierMultiply) {
				resultList.add(modifier.getText()+": *"+modifier.getModValue());
			}
			resultList.add(modifier.getText()+": "+modifier.getModValue());
		}
		return resultList;
	}
	public int getModifierValue(){
		int result = 0;
		for (OneModifier modifier : modifiers) {
			if (modifier instanceof OneModifierMultiply) {
				OneModifierMultiply modifierMultiply = (OneModifierMultiply) modifier;
				result += modifierMultiply.calculateValue(modifiers);
			}else{
				result += modifier.getModValue();
			}
		}
		return result;
	}
	public void setModifiers(List<OneModifier> modifiers) {
		if( ! this.modifiers.equals(modifiers)){
			this.modifiers = modifiers;
			IntStringList intStringList = new IntStringList();
			intStringList.intValue 		= getModifierValue();
			intStringList.stringList 	= getModifierText();
			propertyChange.fireIndexedPropertyChange("Modifiers",PropertyIndexModifiers,null,intStringList);
		}
	}





}